<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/2
 * Time: 下午2:36
 */

namespace app\api\controller;


use app\common\controller\ActionLogs;
use app\common\controller\Balance;
use app\common\controller\Bonus;
use app\common\controller\CardPut;
use app\common\controller\ChildCardModify;
use app\common\controller\TemplateMsg;
use app\common\model\CardConsume;
use app\common\model\CardCzgive;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\CardUserRecLog;
use app\common\model\CardUserUpdate;
use app\common\model\Merchant;
use app\common\model\Paylog;
use Wechat\Wxobjopen;
use Wnkj\WnCardObj;

class Cards extends Pub
{
    //会员卡充值
    public function cardbalancerec($param)
    {
        $ActionLogs = new ActionLogs();
        $ActionLogs->writelog("osapi", "clientlog", "cardbalancerec", "1:", $param);
        try {
            $arr['mid'] = $param['merchantid'];
            $arr['add_balance'] = $param['num'];
            $arr['nickname'] = $param['device_id'];
            $arr['giveid'] = $param['csid'];
            if ($arr['add_balance'] >= 0.01 && $arr['add_balance'] <= 20000 && is_numeric($arr['add_balance'])) {
                //判断充值金额是否为大于0
                if ($arr['add_balance'] > 0 || $arr['giveid'] !== '') {
                    //查询
                    $Merchant = new Merchant();
                    $CardUserGet = new CardUserGet();
                    $top_mid = $Merchant->where('id', $param['merchantid'])->value('top_mid');
                    $midarr = $Merchant->where('top_mid', $top_mid)->field('id')->select();
                    foreach ($midarr as $key => $value) {
                        $smid[] = $value['id'];
                    }
                    $mid = implode(",", $smid);
                    $map['mid'] = ['in', $mid];
                    $map['code'] = $param['card_code'];
                    $user = $CardUserGet->where($map)->find();
                    //END
                    //判断会员信息
                    if ($user) {
                        //判断会员状态是否正常
                        if ($user['status'] == 1) {
                            $bus_orderid = $this->genbusorderid($param['merchantid']);
                            $ComBalance = new Balance();
                            $ComCardPut = new CardPut();
                            $ComBonus = new Bonus();
                            //余额更新
                            $arrc = $ComCardPut->checkgive($arr);
                            //获取更新金额
                            //变动更新
//                        $arr['hx_fs'] = isset($param['pay_fs'])?$param['pay_fs']:4;
                            if (isset($param['pay_fs'])) {
                                $arr['hx_fs'] = $param['pay_fs'] == 10 ? 0 : $param['pay_fs'];
                            } else {
                                $arr['hx_fs'] = 4;
                            }
                            $arr['type'] = 1;
                            $arr['cate'] = 2;
                            $arr['reason'] = 3;
                            if (isset($param['source'])) {
                                $arr['source'] = $param['source'];
                            } else {
                                $arr['source'] = 'API';
                            }
                            $trim_out_trade_no = trim($param['out_trade_no']);
                            if (!empty($trim_out_trade_no)) {
                                $arr['orderid'] = $param['out_trade_no'];
                            } else {
                                $arr['orderid'] = $this->getorderid($arr['mid']);     //订单号
                            }
//                        file_put_contents('/data2/html/actionlog/123.log',json_encode($arr),8);
//                        file_put_contents('/data2/html/actionlog/123.log',json_encode($param),8);


//                        $arr['orderid'] = $this->getorderid($arr['mid']);     //订单号
                            $arr['add_bonus'] = $ComBonus->getrechargebonus($arr['giveid'], $arr['add_balance'], $user['card_id']); //获取赠送积分
                            if (isset($param['pay_fs'])) {
                                $arr['pay_fs'] = $param['pay_fs'] == 10 ? 0 : $param['pay_fs'];
                            } else {
                                $arr['pay_fs'] = 4;
                            }
                            $arr['pay_balance'] = $arrc['pay_balance'];
                            $arr['openid'] = $user['openid'];
                            $arr['card_num'] = $user['code'];
                            $arr['source'] = $param['source'];
                            //订单记录
                            if ($param['pay_cate'] == "Cash") {

                                $datalog = $ComBalance->paylog_rec($arr);
                            } elseif ($param['pay_cate'] == "Electronics") {
                                $maps['orderid'] = $param['out_trade_no'];
                                $datalog = Paylog::where($maps)->find();
                            }
                            $arr['orderid'] = $datalog['orderid'];
//                        $updataid = $ComBalance->cardupdate($arr, $user, $arrc);
                            //充值记录
                            $give_rec_id = $ComBalance->cardczlog_rec($datalog, $user, $arrc, $arr);
                            //冲送
                            $arr['give_rec_id'] = $give_rec_id;
                            $arr['top_mid'] = $user['top_mid'];
//                        $arr['updataid'] = $updataid;
                            $resut = $ComBalance->cs_type($arr, $user);
                            //充值记录
                            $CardUserRecLog = new CardUserRecLog();
                            $recdata['mid'] = $param['merchantid'];
                            $recdata['c_id'] = $user['c_id'];
                            $recdata['us_id'] = $user['id'];
                            $recdata['openid'] = $user['openid'];
                            $recdata['orderid'] = $arr['orderid'];
                            $recdata['give_id'] = $arr['giveid'];
                            if (isset($param['pay_fs'])) {
                                if ($param['pay_fs'] == 10) {
                                    $recdata['pay_cate'] = 0; //支付方式;
                                } else {
                                    $recdata['pay_cate'] = $param['pay_fs'];
                                }
                            } else {
                                $recdata['pay_cate'] = $param['pay_cate'] == 'Cash' ? 4 : 2; //支付方式;
                            }

                            $recdata['source'] = $param['source'];
                            $recdata['action_user'] = $param['device_id'];//操作人
                            $recdata['create_time'] = time();
                            $rec_is = $CardUserRecLog->where(['orderid' => $arr['orderid']])->find($recdata);
                            if (!$rec_is) {
                                $CardUserRecLog->allowField(true)->save($recdata);
                            } else {
                                $rarr['return_code'] = "SUCCESS";
                                $rarr['return_msg'] = '';
                                $rarr['result_code'] = 'SUCCESS';
                                $rarr['result_msg'] = '充值成功';
                                $rarr['record_id'] = $rec_is['id'];
                                $rarr['bus_orderid'] = $bus_orderid;
                                return $rarr;
                            }
                            //END
                            //END
                            $option['rec_id'] = $CardUserRecLog->id;
                            $option['merchantid'] = $param['merchantid'];
                            $option['card_id'] = $user['card_id'];
                            $option['poly'] = 'max';
                            $option['cs_id'] = $arr['giveid'];
                            $option['code'] = $user['code'];
                            $option['orderid'] = $arr['orderid'];
                            $option['num'] = $arrc['add_balance'];
                            $option['bd_cate'] = 'balance';
                            $option['type'] = 1;//变动 1增加 2减少
                            $option['cate'] = 1;  //变动类型
                            //END
                            //支付方式;
                            if (isset($param['pay_fs'])) {
                                if ($param['pay_fs'] == 10) {
                                    $option['pay_cate'] = 0;
                                } else {
                                    $option['pay_cate'] = $param['pay_fs'];
                                }
                            } else {
                                $option['pay_cate'] = $param['pay_cate'] == 'Cash' ? 4 : 2; //支付方式
                            }
                            //END
                            //款台
                            if (isset($param['kuantai'])) {
                                $option['kuantai'] = $param['kuantai'];
                            } else {
                                if ($param['source'] == "PC" || $param['source'] == "pc") {
                                    $option['kuantai'] = $param['device_id'];
                                } else {
                                    $option['kuantai'] = $param['source'];
                                }
                            }
                            //END
                            $option['source'] = $param['source'];  //来源
                            $option['remark'] = '充值'; //备注
                            $option['action_user'] = $param['device_id'];//操作人
                            if (!isset($param['is_notify'])) {
                                $param['is_notify'] = "Y";
                            }
                            if ($param['is_notify'] == "Y") {
                                $option['is_write'] = 'yes';
                            }else{
                                $option['is_write'] = 'no';
                            }
                            $Fztest = new WnCardObj($option);

                            $Fztest->init($bus_orderid);
                            //变动子母卡积分/余额
                            $ChildCardModify = new ChildCardModify();
                            $ChildCardModify->createmodify("balance_rec", $bus_orderid);
                            //END
                            if ($resut) {
                                if (!isset($param['is_notify'])) {
                                    $param['is_notify'] = "Y";
                                }
                                if ($param['is_notify'] == "Y") {
                                    $CreateNotice = new CreateNotify();
                                    $CreateNotice->balance_rec_notify($bus_orderid);
                                }
                                $rarr['return_code'] = "SUCCESS";
                                $rarr['return_msg'] = '';
                                $rarr['result_code'] = 'SUCCESS';
                                $rarr['result_msg'] = '充值成功';
                                $rarr['record_id'] = $option['rec_id'];
                                $rarr['bus_orderid'] = $bus_orderid;
                            } else {
                                $rarr['return_code'] = "SUCCESS";
                                $rarr['return_msg'] = '';
                                $rarr['result_code'] = 'FAIL';
                                $rarr['result_msg'] = '充值记录失败';
                            }
                        } else {
                            $rarr['return_code'] = "SUCCESS";
                            $rarr['return_msg'] = '';
                            $rarr['result_code'] = 'FAIL';
                            $rarr['result_msg'] = '此会员状态异常';
                        }
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'FAIL';
                        $rarr['result_msg'] = '充值失败，请领卡后再进行充值';
                    }
                } else {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = '充值金额必须大于0.01';
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '单次充值金额必须大于0元小于2万元';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;

    }

    //会员卡消费
    public function cardbalancecon($param)
    {
        try {
//            $ActionLogs = new ActionLogs();

            $arr['add_balance'] = $param['num'];
            $arr['mid'] = $param['merchantid'];
            $arr['nickname'] = $param['device_id'];
            $arr['code'] = $param['card_code'];
//            if ($arr['mid'] == 501) {
//                $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "1:", $arr);
//            }
            if ($arr['add_balance'] > 0 && is_numeric($arr['add_balance']) && $arr['add_balance'] <= 20000) {
                //数据库
                $CardUserGet = new CardUserGet();
                $CardMember = new CardMember();
                //查询
                $Merchant = new Merchant();
                $top_mid = $Merchant->where('id', $arr['mid'])->value('top_mid');
//                if ($arr['mid'] == 501) {
//                    $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "2:", $top_mid);
//                }
                $midarr = $Merchant->where('top_mid', $top_mid)->field('id')->select();
//                if ($arr['mid'] == 501) {
//                    $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "3:", $midarr);
//                }
                $smid = [];
                foreach ($midarr as $key => $value) {
                    $smid[] = $value['id'];
                }
//                if ($arr['mid'] == 501) {
//                    $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "4:", $midarr);
//                }
                $mid = implode(",", $smid);
                $map['mid'] = ['in', $mid];
                $map['code'] = $arr['code'];
                $user = $CardUserGet->where($map)->find();
//                if ($arr['mid'] == 501) {
//                    $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "5:", $CardUserGet->getLastSql());
//                }
                $card = $CardMember->where('card_id', $user['card_id'])->find();
                //判断会员卡和会员信息
                if ($card && $user) {
                    //判断会员状态是否正常
                    if ($user['status'] == 1) {
                        //判断消费权限
                        if ($user['is_child'] == "Y") {
                            if ($user['is_shared'] !== "Y") {
                                $rarr['return_code'] = "SUCCESS";
                                $rarr['return_msg'] = '';
                                $rarr['result_code'] = 'FAIL';
                                $rarr['result_msg'] = '没有余额消费权限';
                                return $rarr;
                            }
                        }
                        //END
                        //判断余额
                        if ($arr['add_balance'] <= $user['balance']) {
                            //定义订单号
                            if (isset($param['orderid'])) {
                                $arr['orderid'] = $param['orderid'];
                            } else {
                                $arr['orderid'] = $this->getorderid($arr['mid']);     //订单号
                            }
//                            if ($arr['mid'] == 501) {
//                                $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "6:", $arr);
//                            }
                            $ComBalance = new Balance();
                            //判断赠送金额是否为0   返回需扣除的余额
                            $arrc = $ComBalance->checkbalance($arr, $user, $card);
//                            if ($arr['mid'] == 501) {
//                                $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "11:", $arrc);
//                            }
                            //交易记录
                            $pay_fs = 8;
                            $arr['pay_fs'] = $pay_fs;
                            $arr['openid'] = $user['openid'];                //会员openid
                            $arr['yf_price'] = $arr['add_balance'];        //应付金额
                            $arr['gel_card_zk'] = $arrc['gel_card_zk'];          //抵扣金额
                            $arr['vip_card_zk'] = $arrc['vip_card_zk'];          //等级折扣金额
                            $arr['dis_card_zk'] = $arrc['dis_card_zk'];          //会员折扣金额
                            $arr['mach_zk'] = isset($arrc['mach_zk']) ? $arrc['mach_zk'] : 0;                  //门店折扣金额
                            $arr['hx_card'] = $arrc['hx_card'];                   //抵扣卡券
                            $arr['pay_balance'] = $arrc['del_balance'];     //实际支付额


                            if (isset($param['source'])) {
                                $arr['source'] = $param['source'];
                            } else {
                                $arr['source'] = Paylog::where('orderid', $arr['orderid'])->value('source');
                            }
                            //订单记录
                            $arr['type'] = 2;
//                            if ($arr['mid'] == 501) {
//                                $ActionLogs->writelog("osapi", "clientlog", "cardbalancecon", "12:", $arr);
//                            }
                            $datalog = $ComBalance->paylog_rec($arr);
                            //END
                            //交易记录
                            $arr['hx_fs'] = $pay_fs;
                            $arr['cate'] = 2;
                            $arr['reason'] = 19;
//                    $arr['source'] = 4;
                            $arr['del_balance'] = $arrc['del_balance'];     //实际支付额
//                        $updataid = $ComBalance->cardupdate($arr, $user, $arrc);
                            //END
                            //余额变动记录 交易完成
                            $option['merchantid'] = $param['merchantid'];
                            $option['card_id'] = $user['card_id'];
                            $option['code'] = $user['code'];
                            $option['orderid'] = $arr['orderid'];
                            $option['num'] = $arrc['del_balance'];
                            $option['bd_cate'] = 'balance';
                            $option['type'] = 2;//变动 1增加 2减少
                            $option['cate'] = 2;  //变动类型
                            $option['pay_cate'] = 8; //支付方式
                            //款台
                            if (isset($param['kuantai'])) {
                                $option['kuantai'] = $param['kuantai'];
                            } else {
                                if ($arr['source'] == "PC" || $arr['source'] == "pc") {
                                    $option['kuantai'] = $param['device_id'];
                                } else {
                                    $option['kuantai'] = $arr['source'];
                                }
                            }
                            //END
                            $option['source'] = $arr['source'];  //来源
                            $option['remark'] = '消费'; //备注
                            $option['action_user'] = $param['device_id'];//操作人
                            if (!isset($param['is_notify'])) {
                                $param['is_notify'] = "Y";
                            }
                            if ($param['is_notify'] == "Y") {
                                $option['is_write'] = 'yes';
                            }else{
                                $option['is_write'] = 'no';
                            }
                            $Fztest = new WnCardObj($option);
                            $bus_orderid = $this->genbusorderid($param['merchantid']);
                            $res_id = $Fztest->init($bus_orderid);
                            //变动子母卡积分/余额
                            $ChildCardModify = new ChildCardModify();
                            $ChildCardModify->createmodify("balance_con", $bus_orderid);
                            //END
                            $rarr = $this->getorderinfo($datalog['orderid']);
                            $rarr['record_id'] = $res_id;
                            $rarr['bus_orderid'] = $bus_orderid;
                            if (!isset($param['is_notify'])) {
                                $param['is_notify'] = "Y";
                            }
                            if ($param['is_notify'] == "Y") {
                                $CreateNotice = new CreateNotify();
                                $CreateNotice->balance_con_notify($rarr);
                            }
                        } else {
                            $rarr['return_code'] = "SUCCESS";
                            $rarr['return_msg'] = '';
                            $rarr['result_code'] = 'NOTENOUGH';
                            $rarr['result_msg'] = '消费失败,会员卡余额不足';
                            $rarr['out_trade_no'] = isset($param['orderid']) ? $param['orderid'] : "";
                        }
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'NOCARD';
                        $rarr['result_msg'] = '此会员状态异常';
                    }
                } else {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'NOCARD';
                    $rarr['result_msg'] = '未查询到会员信息';
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '消费失败,请输入正确的消费金额,单次消费金额必须大于0元小于2万元';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

    //卡券核销
    public function cardqcon($param)
    {
//        return $param;
        try {

            $Merchant = new Merchant();
            $top_mid = $Merchant->where('id', $param['merchantid'])->value('top_mid');
            $midlist = $Merchant->where('top_mid', $top_mid)->field('id')->select();
            foreach ($midlist as $k => $v) {
                $midarr[] = $v['id'];
            }
            $mid = implode(',', $midarr);
            $map['mid'] = ['in', $mid];
            $map['top_mid'] = $top_mid;
            $map['code'] = $param['card_code'];
            $map['have'] = 1;
            $map['statu'] = ['neq', 2];
            $map['card_type'] = ['neq', 'MEMBER_CARD'];
            $Wxusergetcard = new CardUserGet();
            $cardinfo = $Wxusergetcard->where($map)->find();
            if ($cardinfo) {
                $data = [
                    'code' => $cardinfo['code'],
                    'card_id' => $cardinfo['card_id'],
                ];
                $con_statu = $this->consumestatu($data['card_id'], $data['code']);
                if ($con_statu == 0) {
                    $options = $this->options($cardinfo['card_id']);
                    $wxObj = new Wxobjopen($options);
                    $res = $wxObj->consumeCode($data);
                    if ($res['errcode'] == 0 && $res['errmsg'] == 'ok') {
                        $data['code'] = $cardinfo['code'];
                        $data['card_type'] = $cardinfo['card_type'];
                        $data['card_id'] = $cardinfo['card_id'];
                        $data['openid'] = $cardinfo['openid'];
                        $data['con_username'] = $param['device_id'];
                        $data['action_user'] = $param['device_id'];//操作人
                        $data['mid'] = $param['merchantid'];
                        $data['consume_info'] = json_encode($res);
                        $data['create_time'] = time();
                        $data['hx_fs'] = $param['source'];
                        $data['source'] = $param['source'];
                        //款台
                        if (isset($param['kuantai'])) {
                            $data['kuantai'] = $param['kuantai'];
                        } else {
                            if ($param['source'] == "PC" || $param['source'] == "pc") {
                                $data['kuantai'] = $param['device_id'];
                            } else {
                                $data['kuantai'] = $param['source'];
                            }
                        }
                        $CardConsume = new CardConsume();
                        $CardConsume->allowField(true)->save($data);
                        $Wxusergetcard->save(['statu' => 2], ['id' => $cardinfo['id']]);
                        //增加核销数量
                        $CardGel = new CardGeneral();
                        $CardGel->where('card_id', $cardinfo['card_id'])->setInc('consume');
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'SUCCESS';
                        $rarr['result_msg'] = '核销成功';
                        $rarr['card_type'] = getcardtype($cardinfo['card_type']);
                        $rarr['cardq_code'] = $cardinfo['code'];;
                        $notify_param['merchantid'] = $param['merchantid'];
                        $notify_param['code'] = $cardinfo['code'];
                        if (!isset($param['is_notify'])) {
                            $param['is_notify'] = "Y";
                        }
                        if ($param['is_notify'] == "Y") {
                            $CreateNotify = new CreateNotify();
                            $CreateNotify->quancon_notify($notify_param);
                        }
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'FAIL';
                        $rarr['result_msg'] = '核销失败。' . $res['errmsg'];

                    }
                } else {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = '核销失败,卡券状态异常';
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '核销失败,卡券编号不存在';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

    //会员卡积分充值
    public function cardbonusrec($param)
    {
        try {
            if ($param['num'] > 0 && is_numeric($param['num']) && floor($param['num']) <= 50000) {
                if (floor($param['num']) == $param['num']) {
                    $CardUserGet = new CardUserGet();
                    $smid = $this->getmidlist('mid', $param['merchantid']);
                    $mid = implode(",", $smid);
                    $map['mid'] = ['in', $mid];
                    $map['code'] = $param['card_code'];
                    $user = $CardUserGet->where($map)->field('id,c_id,card_id,code,openid,status')->find();
                    //判断会员信息
                    if ($user) {
                        //判断会员状态
                        if ($user['status'] == 1) {
                            //充值记录
                            $CardUserRecLog = new CardUserRecLog();
                            $recdata['mid'] = $param['merchantid'];
                            $recdata['c_id'] = $user['c_id'];
                            $recdata['us_id'] = $user['id'];
                            $recdata['openid'] = $user['openid'];
                            $recdata['pay_cate'] = 4;
                            $recdata['source'] = $param['source'];
                            $recdata['action_user'] = $param['device_id'];//操作人
                            $recdata['create_time'] = time();
                            $CardUserRecLog->allowField(true)->save($recdata);
                            //END
                            //款台
                            if (isset($param['kuantai'])) {
                                $option['kuantai'] = $param['kuantai'];
                            } else {
                                if ($param['source'] == "PC" || $param['source'] == "pc") {
                                    $option['kuantai'] = $param['device_id'];
                                } else {
                                    $option['kuantai'] = $param['source'];
                                }
                            }
                            //END
                            $option['rec_id'] = $CardUserRecLog->id;
                            $option['merchantid'] = $param['merchantid'];
                            $option['card_id'] = $user['card_id'];
                            $option['code'] = $user['code'];
                            $option['orderid'] = '';
                            $option['num'] = $param['num'];
                            $option['bd_cate'] = 'bonus';
                            $option['type'] = 1;//变动 1增加 2减少
                            $option['cate'] = 1;  //变动类型
                            $option['pay_cate'] = 4; //支付方式
                            $option['source'] = $param['source'];  //来源
                            $option['remark'] = '充值'; //备注
                            $option['action_user'] = $param['device_id'];//操作人
                            $Fztest = new WnCardObj($option);
                            $bus_orderid = $this->genbusorderid($param['merchantid']);
                            $Fztest->init($bus_orderid);
                            //END
                            //变动子母卡积分/余额
                            $ChildCardModify = new ChildCardModify();
                            $ChildCardModify->createmodify("bonus_rec", $bus_orderid);
                            //END
                            $rarr['record_id'] = $option['rec_id'];
                            $rarr['return_code'] = "SUCCESS";
                            $rarr['return_msg'] = '';
                            $rarr['result_code'] = 'SUCCESS';
                            $rarr['result_msg'] = '充值成功';
                            $rarr['bus_orderid'] = $bus_orderid;
                            if (!isset($param['is_notify'])) {
                                $param['is_notify'] = "Y";
                            }
                            if ($param['is_notify'] == "Y") {
                                $CreateNotify = new CreateNotify();
                                $notify_param['merchantid'] = $param['merchantid'];
                                $notify_param['bus_orderid'] = $bus_orderid;
                                $CreateNotify->bonus_rec_notify($notify_param);
                            }
                        } else {
                            $rarr['return_code'] = "SUCCESS";
                            $rarr['return_msg'] = '';
                            $rarr['result_code'] = 'FAIL';
                            $rarr['result_msg'] = '此会员状态异常';
                        }
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'FAIL';
                        $rarr['result_msg'] = '未查询到会员信息';
                    }
                } else {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = '积分必须是整数';
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '充值积分必须大于0,单次充值积分不可大于5万';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;

    }

    //会员卡积分消费
    /*
     *
     *
     *
     * */
    public function cardbonuscon($param)
    {
        try {
            if ($param['num'] > 0 && floor($param['num']) <= 20000) {
                if (floor($param['num']) == $param['num']) {
                    $arr['bonus'] = $param['num'];
                    $arr['mid'] = $param['merchantid'];
                    $arr['username'] = $param['device_id'];
                    $CardUserGet = new CardUserGet();
                    $smid = $this->getmidlist('mid', $arr['mid']);
                    $mid = implode(",", $smid);
                    $map['mid'] = ['in', $mid];
                    $map['code'] = $param['card_code'];
                    $user = $CardUserGet->where($map)->find();
                    //判断会员信息
                    if ($user) {
                        //判断会员状态
                        if ($user['status'] == 1) {
                            if ($user['bonus'] >= $arr['bonus']) {
                                //判断消费权限
                                if ($user['is_child'] == "Y") {
                                    if ($user['is_shared_bonus'] !== "Y") {
                                        $rarr['return_code'] = "SUCCESS";
                                        $rarr['return_msg'] = '';
                                        $rarr['result_code'] = 'FAIL';
                                        $rarr['result_msg'] = '没有积分消费权限';
                                        return $rarr;
                                    }
                                }
                                //END
                                //款台
                                if (isset($param['kuantai'])) {
                                    $option['kuantai'] = $param['kuantai'];
                                } else {
                                    if ($param['source'] == "PC" || $param['source'] == "pc") {
                                        $option['kuantai'] = $param['device_id'];
                                    } else {
                                        $option['kuantai'] = $param['source'];
                                    }
                                }
                                //END
                                $option['merchantid'] = $param['merchantid'];
                                $option['card_id'] = $user['card_id'];
                                $option['code'] = $user['code'];
                                $option['orderid'] = '';
                                $option['num'] = $param['num'];
                                $option['bd_cate'] = 'bonus';
                                $option['type'] = 2;//变动 1增加 2减少
                                $option['cate'] = 2;  //变动类型
                                $option['pay_cate'] = 8; //支付方式
                                $option['source'] = $param['source'];  //来源
                                $option['remark'] = !empty($param['remark']) ? $param['remark'] : '消费'; //备注
                                $option['action_user'] = $param['device_id'];//操作人
                                $Fztest = new WnCardObj($option);
                                $bus_orderid = $this->genbusorderid($param['merchantid']);
                                $res_id = $Fztest->init($bus_orderid);
                                //END
                                $res['code'] = 0;
                                $res['record_id'] = $res_id;
                                if ($res['code'] == 0) {
                                    //变动子母卡积分/余额
                                    $ChildCardModify = new ChildCardModify();
                                    $ChildCardModify->createmodify("bonus_con", $bus_orderid);
                                    //END
                                    $record_id = $res['record_id'];
                                    $rarr['bus_orderid'] = $bus_orderid;
                                    $rarr['return_code'] = "SUCCESS";
                                    $rarr['return_msg'] = '';
                                    $rarr['result_code'] = 'SUCCESS';
                                    $rarr['result_msg'] = '核销成功';
                                    $rarr['record_id'] = $record_id;
                                    if (!isset($param['is_notify'])) {
                                        $param['is_notify'] = "Y";
                                    }
                                    if ($param['is_notify'] == "Y") {
                                        $CreateNotify = new CreateNotify();
                                        $notify_param['merchantid'] = $param['merchantid'];
                                        $notify_param['bus_orderid'] = $bus_orderid;
                                        $CreateNotify->bonus_con_notify($notify_param);
                                    }

                                } else {
                                    $rarr['return_code'] = "SUCCESS";
                                    $rarr['return_msg'] = '';
                                    $rarr['result_code'] = 'FAIL';
                                    $rarr['result_msg'] = '请输入积分';
                                }
                            } else {
                                $rarr['return_code'] = "SUCCESS";
                                $rarr['return_msg'] = '';
                                $rarr['result_code'] = 'FAIL';
                                $rarr['result_msg'] = '可用积分不足';
                            }
                        } else {
                            $rarr['return_code'] = "SUCCESS";
                            $rarr['return_msg'] = '';
                            $rarr['result_code'] = 'FAIL';
                            $rarr['result_msg'] = '此会员状态异常';
                        }
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'FAIL';
                        $rarr['result_msg'] = '未查询到会员信息';
                    }
                } else {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = '积分必须是整数';
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '核销积分必须大于0,单次核销积分不可大于2万';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

    //创建充送规则
    //
    public function create_czgive($param)
    {
        $Merchant = new Merchant();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
        $maps2['card_id'] = $param['card_id'];
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = CardMember::where($maps2)->count();
        if ($qs) {
            $param['type'] = 1;
            $data['bonus'] = $param['bonus'];
            $data['limit_sume'] = $param['limit_sume'];
            $data['top_mid'] = $merchantinfo['top_mid'];
            $data['mid'] = $merchantinfo['id'];
            $data['card_id'] = $param['card_id'];
            $data['cz_money'] = $param['cz_money'];
            $data['type'] = $param['type'];
            $data['zs_money'] = $param['zs_money'];
            $data['zs_show'] = $param['zs_show'];
            if (isset($param['card_arr']) && !empty($param['card_arr'])) {
                $data['zs_card'] = count($param['card_arr']);
                $data['zs_card_id_all'] = $param['card_arr'];
            } else {
                $data['zs_card_id_all'] = '';
                $data['zs_card'] = 0;
            }
            $data['statu'] = $param['statu'];
            $data['create_time'] = time();
            $CardCzgive = new CardCzgive();
            $res = $CardCzgive->allowField(true)->save($data);
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '创建成功';
            $rarr['info']['cs_id'] = $CardCzgive->getLastInsID();
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
            $rarr['sql'] = $qs;
        }
        return $rarr;

    }

    //创建充送规则

    public function edit_czgive($param)
    {
        $Merchant = new Merchant();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
        $maps2['card_id'] = $param['card_id'];
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = CardMember::where($maps2)->count();
        if ($qs) {
            $data['bonus'] = $param['bonus'];
            $data['limit_sume'] = $param['limit_sume'];
            $data['cz_money'] = $param['cz_money'];
            $data['zs_money'] = $param['zs_money'];
            $data['statu'] = $param['statu'];
            if (isset($arr['zs_card_id'])) {
                $data['zs_card_id'] = implode(",", $param['zs_card_id']);
                if (count($param['zs_card_id']) > 1) {
                    $data['zs_card'] = 2;
                } else {
                    $data['zs_card'] = 1;
                }
            } else {
                $data['zs_card_id'] = '';
                $data['zs_card'] = 0;
            }
            $data['update_time'] = time();
            $map['id'] = $param['id'];
            $CardCzgive = new CardCzgive();
            $res = $CardCzgive->allowField(true)->save($data, $map);
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '修改成功';
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
        }
        return $rarr;

    }

    //删除充送规则
    public function del_czgive($param)
    {
        $Merchant = new Merchant();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
        $maps2['card_id'] = $param['card_id'];
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = CardMember::where($maps2)->count();
        if ($qs) {
            $map['id'] = $param['id'];
            $CardCzgive = new CardCzgive();
            $res = $CardCzgive->where($map)->delete();
            if ($res) {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '删除成功';
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '删除失败';
            }
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
        }
        return $rarr;

    }


    public function cardqlist($param)
    {
        try {
            $CardGeneral = new CardGeneral();
            $map['mid'] = $param['merchantid'];
            $list = $CardGeneral->where($map)->field('id,title as name')->select();
            $total = $CardGeneral->where($map)->count();
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '获取成功';
            $rarr['total'] = $total;
            $rarr['info'] = $list;
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;

    }

}